/*
 * @Author: your name
 * @Date: 2020-12-30 14:09:14
 * @LastEditTime: 2021-01-08 15:02:53
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \node-server\db\index.js
 */
const mysql =require('mysql')
const {
    host,
    user,
    password,
    database } = require('./config')
const { IS_LOG : debug} = require('../utils/constant')
const connect = () => {
    return mysql.createConnection({
        host,
        user,
        password,
        database,
        multipleStatements: true
    })
}
const querySql = (sql) => {
    const conn = connect()
    console.log('执行的sql语句是：',sql)
    return new Promise((resolve, reject) => {
        try {
            conn.query(sql, (err, results) => {
                if (err) {
                    debug && console.log('SQL执行 失败，原因:' + JSON.stringify(err))
                    reject({
                        status:false,//失败
                        err
                    })
                } else {
                    debug && console.log('SQL执行 成功', JSON.stringify(results))
                    resolve({
                        status:true,//成功
                        results
                    })
                }
            })
        } catch (e) {
            reject(e)
        } finally {
            conn.end()
        }
    })
}

module.exports ={
    querySql
}